/*
 * SpeedoDyno - Speedometer and dynamometer application for Android 
 * Copyright 2013 Guilhem VALENTIN
 * 
 * This file is part of SpeedoDyno
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with SpeedoDyno. If not, see <http://www.gnu.org/licenses/>.
 */
package com.gvalentin.speedodyno.data;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.location.Location;

import com.gvalentin.speedodyno.util.FileUtil;

/**
 * Log data in file
 * @author guilhem
 */
public class DataLogger {

    // Date formatting tool
	private static final SimpleDateFormat dfiso = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.ENGLISH);


    /**
     * Store the location on disk
     */
	public static void storeLocation(Location location, SpeedoDynoData data) {
		if (location == null || data == null) {
			return;
		}
		
		File file = FileUtil.getCsvLogFile("location");
		StringBuilder buffer = new StringBuilder();
		
		if (!file.exists()) {
			buffer.append("Time");
			buffer.append(";GPS Time;GPS Provider;GPS Latitude;GPS Longitude;GPS Distance;GPS Altitude;GPS Speed;GPS Bearing;GPS Accuracy");
			buffer.append(";CSpeed;CSpeedAvg;CSpeedMax;CAccel;CInclineDeg;CInclinePer;CPower;CPowerFriction;CPowerAero;CPowerAccel;CPowerElevation;CPowerAvg");
			buffer.append("\n");
		}
		buffer.append(dfiso.format(new Date(System.currentTimeMillis())));
		// Location data
		buffer.append(";").append(dfiso.format(new Date(location.getTime())));
		buffer.append(";").append(location.getProvider());
		buffer.append(";").append(location.getLatitude());
		buffer.append(";").append(location.getLongitude());
		buffer.append(";").append(data.previous != null ? location.distanceTo(data.previous) : 0.0f);
		buffer.append(";").append(location.getAltitude());
		buffer.append(";").append(location.getSpeed());
		buffer.append(";").append(location.getBearing());
		buffer.append(";").append(location.getAccuracy());
		// Calculated data
		buffer.append(";").append(data.getSpeed());
		buffer.append(";").append(data.getSpeedAverage());
		buffer.append(";").append(data.getSpeedMax());
		buffer.append(";").append(data.getAcceleration());
		buffer.append(";").append(data.getInclineDeg());
		buffer.append(";").append(data.getInclinePer());
		buffer.append(";").append(data.getPower());
		buffer.append(";").append(data.getPowerFriction());
		buffer.append(";").append(data.getPowerAero());
		buffer.append(";").append(data.getPowerAccel());
		buffer.append(";").append(data.getPowerElevation());
		buffer.append(";").append(data.getPowerAverage());
		
		buffer.append("\n");
		
		FileUtil.writeCsvToFile(file, buffer);
	}
	
}
